#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>

struct ListNode {
	int val;
	struct ListNode* next;
};

typedef struct ListNode Node;
struct ListNode* removeElements(struct ListNode* head, int val) {
    Node* newhead, * newtail;
    newhead = newtail = NULL;
    Node* pcur = head;
    while (pcur)
    {
        if (pcur->val != val)
        {
            if (newhead == NULL)
            {
                newhead = newtail = pcur;
            }
            else {
                newtail->next = pcur;
                newtail = newtail->next;
            }
        }
        pcur = pcur->next;
    }
    if (newtail)
    {
        newtail->next = NULL;
    }
    return newhead;
}